package useless;

import useless.Axis3DPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Line2D;
import java.awt.geom.Ellipse2D;

public class draw extends JPanel {
    private final int[][] data = {
            {0, 5, 517},
            {1, 4, 716},
            {2, 3, 777},
            {3, 2, 1000},
            {4, 1, 1148},
            {5, 0, 1242}
    };

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        int width = getWidth();
        int height = getHeight();

        int centerX = width / 2;
        int centerY = height / 2;
        int axisLength = 200;

        // 画坐标轴
        drawLine(g2d, centerX, centerY, centerX + axisLength, centerY, Color.RED, "bf");
        drawLine(g2d, centerX, centerY, centerX, centerY - axisLength, Color.GREEN, "tf");
        drawLine(g2d, centerX, centerY, centerX - axisLength / 2, centerY + axisLength / 2, Color.BLUE, "P");

        // 绘制数据点
        for (int[] point : data) {
            int bf = point[0];
            int tf = point[1];
            int p = point[2]; // 概率，单位是千分之一
            drawPoint(g2d, centerX + bf * 40, centerY - tf * 40, centerX - p / 100, centerY + p / 100, Color.BLACK);
        }
    }

    private void drawLine(Graphics2D g2d, int x1, int y1, int x2, int y2, Color color, String label) {
        g2d.setColor(color);
        g2d.draw(new Line2D.Double(x1, y1, x2, y2));
        g2d.drawString(label, x2 + 5, y2 - 5);
    }

    private void drawPoint(Graphics2D g2d, int x, int y, int zX, int zY, Color color) {
        g2d.setColor(color);
        g2d.fill(new Ellipse2D.Double(zX - 3, zY - 3, 6, 6));
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("3D Probability Scatter Plot");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(500, 500);
            frame.add(new Axis3DPanel());
            frame.setVisible(true);
        });
    }
}
